parameters:
  buildConfig: ''
  archType: ''
  osGroup: ''
  osSubgroup: ''
  container: ''
  crossBuild: false
  timeoutInMinutes: ''
  variables: {}
  pool: ''
  condition: true

### Format job
jobs:
- template: /eng/pipelines/common/templates/runtimes/xplat-job.yml
  parameters:
    buildConfig: ${{ parameters.buildConfig }}
    archType: ${{ parameters.archType }}
    osGroup: ${{ parameters.osGroup }}
    osSubgroup: ${{ parameters.osSubgroup }}
    container: ${{ parameters.container }}
    crossBuild: ${{ parameters.crossBuild }}
    timeoutInMinutes: ${{ parameters.timeoutInMinutes }}
    name: ${{ format('format_{0}{1}_{2}', parameters.osGroup, parameters.osSubgroup, parameters.archType) }}
    displayName: ${{ format('Formatting {0}{1} {2}', parameters.osGroup, parameters.osSubgroup, parameters.archType) }}
    helixType: 'format'
    pool: ${{ parameters.pool }}
    variables:
    - template: /eng/pipelines/coreclr/templates/jit-python-variables.yml
      parameters:
        osGroup: ${{ parameters.osGroup }}
    - ${{ each variable in parameters.variables }}:
      - ${{insert}}: ${{ variable }}

    - ${{ if eq(parameters.osGroup, 'linux') }}:
      - name: LinuxCrossArg
        value: '--cross'
    - ${{ if ne(parameters.osGroup, 'linux') }}:
      - name: LinuxCrossArg
        value: ''

    condition: and(succeeded(), ${{ parameters.condition }})

    steps:

    - task: UseDotNet@2
      # This should match what jitutils YML uses to build.
      displayName: 'Install .NET SDK'
      inputs:
        packageType: 'sdk'
        version: '6.x'
        includePreviewVersions: true
        installationPath: $(Agent.ToolsDirectory)/dotnet

    - script: $(PythonSetupScript)
      displayName: Enable python venv

    - script: $(PythonScript) $(Build.SourcesDirectory)/src/coreclr/scripts/jitformat.py -r $(Build.SourcesDirectory) -o $(osGroup) -a $(archType) $(LinuxCrossArg)
      displayName: Run jitformat.py

    - task: PublishBuildArtifacts@1
      displayName: Publish format.patch
      inputs:
        PathtoPublish: '$(Build.SourcesDirectory)/format.patch'
        ArtifactName: format.$(osGroup).$(archType).patch
      continueOnError: true
      condition: failed()
